package com.useeinfo.oa.modules.maintaining.dao;

import com.useeinfo.framework.extend.dao.CrudDao;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.data.QueryUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.maintaining.entity.PlannerOrder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Date: 2018/12/28 12:05
 * @Description: 计划工单dao
 */
@Repository("plannerOrderDao")
public class PlannerOrderDao implements CrudDao<PlannerOrder> {

    private final static Logger logger = LoggerFactory.getLogger(PlannerOrderDao.class);

    @PersistenceContext
    private EntityManager entityManager;


    @Override
    public Map<String, Object> getSearchCondition(Map<String, String> queryHash) {
        Map<String, Object> conditionHash = new HashMap<>();
        conditionHash.put("s.deleteStatus = ?{paramIndex} ", false);
        if (queryHash == null || queryHash.size() == 0) {
            return conditionHash;
        }
        for(String key:queryHash.keySet()){
            String value = queryHash.get(key);
            switch (key){
                case "name":
                    conditionHash.put("s.name like ?{paramIndex}","%"+value+"%");
                    continue;
                case "enterpriseId":
                    conditionHash.put("s.enterprise.id = ?{paramIndex}",StringConverters.null2Long(value));
                    continue;
                case "searchCode":
                    conditionHash.put(" (s.name like ?{paramIndex} or s.serial like ?{paramIndex} or s.required like ?{paramIndex} or " +
                            "s.degree like ?{paramIndex} or s.type like ?{paramIndex} or s.organization.organizationName like ?{paramIndex} )","%"+value+"%");
                    continue;
                case "required":
                    conditionHash.put("s.required like ?{paramIndex}",value);
                    continue;
                case "start":
                    conditionHash.put("s.startTime >= ?{paramIndex}",StringConverters.ToDateOnly(value));
                    continue;
                case "end":
                    conditionHash.put("s.endTime <= ?{paramIndex}",StringConverters.ToDateOnly(value));
                    continue;
                case "period":
                    conditionHash.put("s.periodInstall.name like ?{paramIndex}",value);
                    continue;
                case "orderName":
                    conditionHash.put("s.name = ?{paramIndex}",value);
                    continue;

            }
        }

        return conditionHash;
    }

    @Override
    public Long totalRecord(Map<String, String> queryHash) {
        Map<String, Object> conditions = getSearchCondition(queryHash);
        TypedQuery<Long> typedQuery = QueryUtils.getTypedQueryByCondition("select count(s) from PlannerOrder s ", conditions, entityManager, Long.class);
        return typedQuery.getSingleResult();
    }

    @Override
    public List<PlannerOrder> findList(QueryParam queryParam) {
        String sqlInfo = queryParam.joinJPQL("select s from PlannerOrder s ", "order by s.id desc ");
        Map<String, Object> conditions = getSearchCondition(queryParam.getSqlMap());
        TypedQuery<PlannerOrder> typedQuery = QueryUtils.getTypedQueryByCondition(sqlInfo, conditions, entityManager, PlannerOrder.class);
        return queryParam.findPageList(typedQuery);
    }

    @Override
    public PlannerOrder findModel(Long userId) {
        return entityManager.find(PlannerOrder.class, userId);
    }

    @Override
    public Integer add(PlannerOrder plannerOrder) {
        plannerOrder.setCreateDate(new Date());
        entityManager.persist(plannerOrder);
        logger.info("PlannerOrderDaoImpl添加plannerOrder成功！");
        return 1;
    }

    @Override
    public Integer update(PlannerOrder plannerOrder) {
        plannerOrder.setUpdateDate(new Date());
        entityManager.merge(plannerOrder);
        return 1;
    }

    @Override
    public Integer delete(Long plannerOrderId) {
        PlannerOrder plannerOrder = entityManager.find(PlannerOrder.class, plannerOrderId);
        entityManager.remove(plannerOrder);
        return 1;
    }

    @Override
    public Integer deleteLogic(PlannerOrder model) {
        model.setDeleteStatus(true);
        return 1;
    }

    public List<PlannerOrder> hqlExcute(String hql) {
        return entityManager.createQuery(hql,PlannerOrder.class).getResultList();
    }

}
